package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.MatStationMapper;
import com.indusfo.spc.pojo.MatDete;
import com.indusfo.spc.pojo.MatStation;
import com.indusfo.spc.service.MatStationService;
import com.indusfo.spc.vo.HintMessage;
import com.indusfo.spc.vo.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author liub
 * @Date 2019/11/21 10:37
 * @Version 1.0
 */
@Service
public class MatStationServiceImpl implements MatStationService {

    // 声明日志的静态变量
    private static final Logger logger = LoggerFactory.getLogger(MatStationServiceImpl.class);

    @Resource
    private MatStationMapper matStationMapper;

    //查询
    @Override
    public JSONObject queryMatStation(MatStation matStation) {
        try {
            //获取页面的数据个数
            Integer pagesize = matStation.getPagesize();
            //获取查询的页数
            Integer pageindex = matStation.getPageindex();
            if (pagesize != null && pageindex != null) {
                matStation.setIncept(pagesize * (pageindex - 1));
            }
            //执行查询操作
            List<MatStation> matStationList =new ArrayList<>();
            if(matStation.getMatFlowId()==null){
                return JSONObject.oK(HintMessage.QUERY_NULL, matStationList, 0);
            }
            matStationList = matStationMapper.queryMatStation(matStation);

            //如果页数和页面数量都不为空的情况下进行计数
            if (matStationList.isEmpty()) {
                return JSONObject.oK(HintMessage.QUERY_NULL, matStationList, 0);
            } else {
                Integer count = matStationMapper.countMatStation(matStation);
                return JSONObject.oK(HintMessage.QUERY_SUCCESS, matStationList, count);
            }

        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());

        }
    }

    //新增
    @Override
    public JSONObject insertMatStation(MatStation matStation) {
        try {
            if (matStation.getMatStationId() != null) {
                throw new ModifyFailedException(HintMessage.INSERT_ID);
            }
            //初始化基础数据
            if (matStation.getDataState() == null) {
                matStation.setDataState(1);
            }
            //判重
            checkParam(matStation);
            //执行新增操作
            Integer row = matStationMapper.insertMatStation(matStation);
            if (row == 0) {
                throw new ModifyFailedException(HintMessage.INSERT_DEFEATED);
            }
            return JSONObject.oK(HintMessage.INSERT_SUCCESS);
        } catch (GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    //修改
    @Override
    public JSONObject updateMatStation(MatStation matStation) {
        try {
            if (matStation.getMatStationId() == null) {
                throw new ParamsErrorException(HintMessage.UPDATE_ID);
            }
            //初始化基础数据
            if (matStation.getDataState() == null) {
                matStation.setDataState(1);
            }
            //判重
            checkParam(matStation);
            //执行修改操作
            int row = matStationMapper.updateMatStation(matStation);
            if (row == 0) {
                throw new ModifyFailedException(HintMessage.UPDATE_DEFEATED);
            }
            return JSONObject.oK(HintMessage.UPDATE_SUCCESS);
        } catch (GlobalException e) {//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    //删除
    @Override
    public JSONObject deleteMatStation(List<Integer> matStationIds, Integer dataState) {
        try {
            //空值判断
            if (dataState == null) {
                throw new ParamsErrorException(HintMessage.DATASTATE_NULL);
            }
            if (matStationIds == null) {
                throw new ParamsErrorException(HintMessage.LIST_NULL);
            }
            //记录异常信息
            String msg = "";
            switch (dataState) {
                case 1:
                    msg = HintMessage.START_VALUE;
                    break;
                case 2:
                    msg = HintMessage.DELETE_VALUE;
                    break;
                case 3:
                    msg = HintMessage.STOP_VALUE;
                    break;
                default:
                    throw new ParamsErrorException(HintMessage.VIOLATION_OPERATION);
            }
            //执行删除操作
            int row = matStationMapper.deleteMatStation(matStationIds, dataState);
            //声明msg字符串，用来存抛出的异常
            if (row == 0) {
                msg = msg + HintMessage.DEFEATED_VALUE;
            } else {
                msg = msg + HintMessage.SUCCESS_VALUE;
            }
            return JSONObject.oK(msg);
        } catch (GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    //判重
    public void checkParam(MatStation matStation) {
        Integer matFlowId = matStation.getMatFlowId();
        Integer stationId = matStation.getStationId();
        //产品流程ID
        if (matFlowId == null) {
            throw new ParamsErrorException(HintMessage.MATFLOWID_NULL);
        }
        //工位ID
        if (stationId == null) {
            throw new ParamsErrorException(HintMessage.STATIONID_NULL);
        }
        // 执行判重操作
        Integer Counts = matStationMapper.checkMatStation(matStation);
        if (Counts != 0) {
            throw new ParamsErrorException(HintMessage.REPEATING_DATA);
        }

    }

}
